package com.example.devtool.generate.service.impl;

import cn.hutool.json.JSONUtil;
import com.example.devtool.core.exception.BizException;
import com.example.devtool.generate.controller.request.GenerateRequest;
import com.example.devtool.generate.entity.GenerateHistory;
import com.example.devtool.generate.service.GenerateService;
import com.example.devtool.generate.service.IGenerateHistoryService;
import com.example.devtool.generate.util.GenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhuliangxuan
 * @date 2022/5/22 21:48
 */
@Service
public class GenerateServiceImpl implements GenerateService {
    @Autowired
    private IGenerateHistoryService iGenerateHistoryService;

    @Override
    public void generate(GenerateRequest request) {
        GenerateHistory history = new GenerateHistory();
        try {
            history.setCreator("admin");
            history.setTraceId(request.getTraceId());
            String param = JSONUtil.toJsonStr(request);
            history.setCreateParam(param);
            history.setStatus(1);
            GenerateUtil.mybatisGenerate(request);
            iGenerateHistoryService.save(history);
        }catch (Exception e) {
            history.setStatus(0);
            history.setErrorMsg(e.getMessage());
            iGenerateHistoryService.save(history);
            throw new BizException("generate", "代码生成失败", e.getMessage());
        }
    }
}
